package ru.usu.gv.gui.frames.styles;

import ru.usu.gv.core.ResourceLoader;
import ru.usu.gv.gui.component.IDropDownItem;
import ru.usu.gv.gui.frames.ISaveCallback;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;

/**
 * @author spupyrev
 * 02.11.2008
 * 
 * TODO: move some methods???
 */
public class GUIStyleUtilities
{
	public static ActionListener getChooseColorListener(final JLabel colorLabel)
	{
		return new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Color choosedColor = JColorChooser.showDialog(colorLabel, "Выберите цвет", colorLabel.getBackground());
				if (choosedColor != null)
					colorLabel.setBackground(choosedColor);
			}
		};
	}

	public static AbstractAction createEmptyAction()
	{
		return new AbstractAction()
		{
			public void actionPerformed(ActionEvent e)
			{
			}
		};
	}

	public static ActionListener createApplyListener(final Window window, final List<ISaveCallback> saveCallbacks)
	{
		return new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				for (ISaveCallback callback : saveCallbacks)
					callback.doSave();

				window.dispose();
			}
		};
	}

	public static ActionListener createCancelListener(final Window window)
	{
		return createApplyListener(window, Collections.EMPTY_LIST);
	}

	public static IDropDownItem createDropDownItem(final String iconUrl, final String title, final String id)
	{
		return new IDropDownItem()
		{
			public Icon getIcon()
			{
				return ResourceLoader.loadImage(iconUrl);
			}

			public String getTitle()
			{
				return title;
			}

			public String getValue()
			{
				return id;
			}
		};
	}

}
